package cn.hasayam.test3;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Random;

public class Setting extends AppCompatActivity {

    RadioGroup rg1;
    RadioGroup rg2;
    RadioButton r1;
    RadioButton r2;
    RadioButton r3;
    RadioButton r4;
    Switch xuan;
    Context context = Setting.this;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_setting);

        rg1 = findViewById(R.id.rg1);
        rg2 = findViewById(R.id.rg2);
        r1 = findViewById(R.id.r1);
        r2 = findViewById(R.id.r2);
        r3 = findViewById(R.id.r3);
        r4 = findViewById(R.id.r4);
        xuan = findViewById(R.id.xuan);

        rg1.setOnCheckedChangeListener(listen1);
        rg2.setOnCheckedChangeListener(listen2);


        new Thread(new Runnable() {
            @Override
            public void run() {
                final String l = SettingDatabase.getInstance(context).getSettingDao().getValue("language");
                final String x = SettingDatabase.getInstance(context).getSettingDao().getValue("xuan");
                final String s = SettingDatabase.getInstance(context).getSettingDao().getValue("style");
                if (l == null || x == null || s == null){
                    SettingBean s1 = new SettingBean("language","ug");
                    SettingBean s2 = new SettingBean("xuan","false");
                    SettingBean s3 = new SettingBean("style","1");
                    SettingDatabase.getInstance(context).getSettingDao().insert(s1,s2,s3);
                    Setting.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            rg1.check(R.id.r1);
                            xuan.setChecked(false);
                            rg2.check(R.id.r3);
                        }
                    });
                }
                else {
                    Setting.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            switch (l) {
                                case "ug": rg1.check(R.id.r1);break;
                                case "cn": rg1.check(R.id.r2);break;
                            }
                            switch (x) {
                                case "true": xuan.setChecked(true);break;
                                case "false": xuan.setChecked(false);break;
                            }
                            switch (s) {
                                case "1": rg2.check(R.id.r3);break;
                                case "2": rg2.check(R.id.r4);break;
                            }
                        }
                    });
                }
            }
        }).start();

        xuan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    Log.i("开关","开");
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            SettingDatabase.getInstance(context).getSettingDao().update(new SettingBean("xuan","true"));
                        }
                    }).start();

                }else {
                    Log.i("开关","关");
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            SettingDatabase.getInstance(context).getSettingDao().update(new SettingBean("xuan","false"));
                        }
                    }).start();
                }
            }
        });
    }

    private RadioGroup.OnCheckedChangeListener listen1=new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (group.getCheckedRadioButtonId()) {
                case R.id.r1:
                    Log.i("选项","1");
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            SettingDatabase.getInstance(context).getSettingDao().update(new SettingBean("language","ug"));
                        }
                    }).start();
                    break;
                case R.id.r2:
                    Log.i("选项","2");
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            SettingDatabase.getInstance(context).getSettingDao().update(new SettingBean("language","cn"));
                        }
                    }).start();
                    break;
            }
        }
    };
    private RadioGroup.OnCheckedChangeListener listen2=new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (group.getCheckedRadioButtonId()) {
                case R.id.r3:
                    Log.i("选项","3");
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            SettingDatabase.getInstance(context).getSettingDao().update(new SettingBean("style","1"));
                        }
                    }).start();
                    break;
                case R.id.r4:
                    Log.i("选项","4");
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            SettingDatabase.getInstance(context).getSettingDao().update(new SettingBean("style","2"));
                        }
                    }).start();
                    break;
            }
        }
    };
    @Override
    public void onBackPressed(){
        Intent intent = new Intent(Setting.this,MainActivity.class);
        startActivity(intent);
        finish();
    }
    public void exit(View view) {
        Intent intent = new Intent(Setting.this,MainActivity.class);
        startActivity(intent);
        finish();
    }

    public void nurali(View view) {
        String makal ="";
        try {
            AssetManager assetManager = getAssets(); //获得assets资源管理器（assets中的文件无法直接访问，可以使用AssetManager访问）
            InputStreamReader inputStreamReader = new InputStreamReader(assetManager.open("makal.txt"),"UTF-8"); //使用IO流读取json文件内容
            BufferedReader br = new BufferedReader(inputStreamReader);//使用字符高效流
            String line;
            StringBuilder builder = new StringBuilder();
            while ((line = br.readLine())!=null){
                builder.append(line);
            }
            br.close();
            inputStreamReader.close();

            String all = builder.toString();
            String[] s = all.split("-");
            Log.e("sss",""+s[0]);
            Random random = new Random();
            int i = random.nextInt(s.length);
            makal = s[i].replace("،","\n");
        } catch (IOException e) {
            e.printStackTrace();
        }


        @SuppressLint("ResourceType")
        InputStream is = getResources().openRawResource(R.drawable.weixin);
        Bitmap bitmap = BitmapFactory.decodeStream(is);
        ImageDialog dialog = new ImageDialog(this);
        dialog.addBitmap(bitmap);
        dialog.addTitle(makal);

        dialog.show();
    }
}
